﻿using HuaCheng.DataAccess;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using System.Data;
using HuaCheng.SendAndReceive;
using System.Windows.Forms;
using HuaCheng.CommonClass;
using System.Text;

namespace HuaCheng.Controller
{
    class StepRunControl
    {
        /// <summary>
        /// 独立参数包的固定值设置
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="inPara"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependParameter InParaFixedValueSet(StepSetInfo sp, CommandProtocol.IndependParameter inPara)
        {
            inPara.GID = sp.Gid - 1;
            inPara.CID = sp.Cid - 1;
            inPara.Mid = sp.Mid - 1;
            try
            {
                inPara.dcBusVoltSet = (int)(Convert.ToSingle(DicHelper.RetrieveBusUnderVolt()) * Convert.ToSingle(DicHelper.RetrieveMagnification()));
                inPara.chEnable = true;
                if (DicHelper.RetrieveBatteryTest() == "1")
                {
                    inPara.isBattery = true;
                }
                else
                {
                    inPara.isBattery = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return inPara;
        }
        /// <summary>
        /// 独立动作包的固定值设置
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="inPara"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction InActionFixedValueSet(StepSetInfo sp, CommandProtocol.IndependAction inAction)
        {
            inAction.GID = sp.Gid - 1;
            inAction.CID = sp.Cid - 1;
            inAction.Mid = sp.Mid - 1;
            inAction.fan = 1;
            return inAction;
        }
        /// <summary>
        /// 独立脉冲工况包的固定值设置
        /// </summary>
        /// <param name="ps"></param>
        /// <param name="inCondition"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependWorkCondition InConditionFixedValueSet(StepSetInfo sp, CommandProtocol.IndependWorkCondition inCondition)
        {
            inCondition.GID = sp.Gid - 1;
            inCondition.CID = sp.Cid - 1;
            inCondition.Mid = sp.Mid - 1;
            return inCondition;
        }
        /// <summary>
        /// 独立模式的系统复位
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentSystemReset(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            //系统复位
            inAction.systemReset = 1;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return inAction;
        }
        /// <summary>
        /// 独立模式的系统复位撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentSystemResetBack(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            inAction.systemReset = 0;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            //Thread.Sleep(10);
            return inAction;
        }
        /// <summary>
        /// 独立模式的开机
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentOpen(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            inAction.onOff = CommandProtocol.EOnOffState.On;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            //Thread.Sleep(10);
            return inAction;
        }
        /// <summary>
        /// 独立模式的开机撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentOpenBack(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            inAction.onOff = CommandProtocol.EOnOffState.Invalid;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            //Thread.Sleep(10);
            return inAction;
        }
        /// <summary>
        /// 独立模式的启动
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentStart(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            //启动指令下达
            inAction.startPause = CommandProtocol.EStartPauseState.Start;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return inAction;
        }
        /// <summary>
        /// 独立模式启动撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentStartBack(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            inAction.startPause = CommandProtocol.EStartPauseState.Invalid;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return inAction;
        }
        /// <summary>
        /// 独立模式暂停
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <param name="stepNum"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentStop(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            inAction.startPause = CommandProtocol.EStartPauseState.Pause;
            inAction.controlReset = 1;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return inAction;
        }
        /// <summary>
        /// 独立模式暂停撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentStopBack(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            inAction.startPause = CommandProtocol.EStartPauseState.Invalid;
            inAction.controlReset = 0;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return inAction;
        }
        /// <summary>
        /// 独立模式关机
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <param name="stepNum"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentClose(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {
            if (client != null)
            {
                inAction.onOff = CommandProtocol.EOnOffState.Off;
                inAction.fan = 0;
                temp = inAction.Command();
                string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

                StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            }
            return inAction;
        }
        /// <summary>
        /// 独立模式关机撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <param name="stepNum"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependAction IndependentCloseBack(CommandProtocol.IndependAction inAction, byte[] temp, Socket client)
        {

            inAction.onOff = CommandProtocol.EOnOffState.Invalid;
            temp = inAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return inAction;
        }
        /// <summary>
        /// 并联参数包的固定值设置
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="inPara"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleParameter PaParaFixedValueSet(StepSetInfo sp, CommandProtocol.ParalleParameter paPara)
        {
            paPara.GID = sp.Gid - 1;
            paPara.CID = sp.Cid - 1;
            paPara.Mid = sp.Mid - 1;
            paPara.paraMode = 1;
            paPara.shareVolt = sp.Cid - 1;
            paPara.dcBusVoltSet = Convert.ToInt32(DicHelper.RetrieveBusUnderVolt()) * Convert.ToInt32(DicHelper.RetrieveMagnification());
            paPara.chEnable = true;
            if (DicHelper.RetrieveBatteryTest() == "1")
            {
                paPara.isBattery = true;
            }
            else
            {
                paPara.isBattery = false;
            }
            return paPara;
        }
        /// <summary>
        /// 并联动作包的固定值设置
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="inPara"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction PaActionFixedValueSet(StepSetInfo sp, CommandProtocol.ParalleAction paAction)
        {
            paAction.GID = sp.Gid - 1;
            paAction.CID = sp.Cid - 1;
            paAction.Mid = sp.Mid - 1;
            paAction.fan = 1;
            return paAction;
        }
        /// <summary>
        /// 并联工况包的固定值设置
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="paCondition"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleWorkCondition PaConditionFixedValueSet(StepSetInfo sp, CommandProtocol.ParalleWorkCondition paCondition)
        {
            paCondition.GID = sp.Gid - 1;
            paCondition.CID = sp.Cid - 1;
            paCondition.Mid = sp.Mid - 1;
            return paCondition;
        }
        /// <summary>
        /// 并联使能包的固定值设置
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="paPulse"></param>
        /// <returns></returns>
        public static CommandProtocol.ParallePulseEner PaPulseFixedValueSet(StepSetInfo sp, CommandProtocol.ParallePulseEner paPulse)
        {
            paPulse.GID = sp.Gid - 1;
            paPulse.CID = sp.Cid - 1;
            paPulse.Mid = sp.Mid - 1;
            return paPulse;
        }
        /// <summary>
        /// 并联模式的系统复位
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleSystemReset(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            //系统复位
            paAction.systemReset = 1;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paAction;
        }
        /// <summary>
        /// 并联模式的系统复位撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleSystemResetBack(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.systemReset = 0;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            //Thread.Sleep(10);
            return paAction;
        }
        /// <summary>
        /// 并联模式的开机
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleOpen(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.onOff = CommandProtocol.EOnOffState.On;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));
            //Thread.Sleep(10);
            return paAction;
        }
        /// <summary>
        /// 并联模式的开机撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="stepNum"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleOpenBack(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.onOff = CommandProtocol.EOnOffState.Invalid;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            //Thread.Sleep(10);
            return paAction;
        }
        /// <summary>
        /// 并联模式的启动
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleStart(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            //启动指令下达
            paAction.startPause = CommandProtocol.EStartPauseState.Start;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paAction;
        }
        /// <summary>
        /// 并联模式的启动撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleStartBack(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.startPause = CommandProtocol.EStartPauseState.Invalid;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paAction;
        }
        /// <summary>
        /// 并联模式的暂停
        /// </summary>
        /// <param name="paAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleStop(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.startPause = CommandProtocol.EStartPauseState.Pause;
            paAction.controlReset = 1;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paAction;
        }
        /// <summary>
        /// 并联模式暂停撤回
        /// </summary>
        /// <param name="paAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleStopBack(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.startPause = CommandProtocol.EStartPauseState.Invalid;
            paAction.controlReset = 0;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paAction;
        }
        /// <summary>
        /// 并联模式关机
        /// </summary>
        /// <param name="paAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <param name="stepNum"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleClose(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            if (client != null)
            {
                paAction.onOff = CommandProtocol.EOnOffState.Off;
                paAction.fan = 0;
                temp = paAction.Command();
                string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

                StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            }
            return paAction;
        }
        /// <summary>
        /// 并联模式关机撤回
        /// </summary>
        /// <param name="inAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <param name="stepNum"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleAction ParalleCloseBack(CommandProtocol.ParalleAction paAction, byte[] temp, Socket client)
        {
            paAction.onOff = CommandProtocol.EOnOffState.Invalid;
            temp = paAction.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paAction;
        }
        /// <summary>
        /// 并联使能包下发
        /// </summary>
        /// <param name="paAction"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <param name="stepNum"></param>
        /// <returns></returns>
        public static CommandProtocol.ParallePulseEner ParallePulse(CommandProtocol.ParallePulseEner paPulse, byte[] temp, Socket client)
        {
            paPulse.pulseEn = 1;
            temp = paPulse.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paPulse;
        }
        /// <summary>
        /// 并联使能包撤回
        /// </summary>
        /// <param name="paPulse"></param>
        /// <param name="temp"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static CommandProtocol.ParallePulseEner ParallePulseBack(CommandProtocol.ParallePulseEner paPulse, byte[] temp, Socket client)
        {
            paPulse.pulseEn = 0;
            temp = paPulse.Command();
            string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

            StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

            return paPulse;
        }
        /// <summary>
        /// 发送下一笔工况数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="dt"></param>
        /// <param name="beginWaitReq"></param>
        /// <param name="runState"></param>
        /// <param name="temp"></param>
        /// <param name="inCondition"></param>
        /// <param name="paCondition"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static StepRunBool SendNextConditionData(int count, DataTable dt, DateTime beginWaitReq, StepRunBool runState, byte[] temp, CommandProtocol.IndependWorkCondition inCondition, CommandProtocol.ParalleWorkCondition paCondition, Socket client, int paraCount)
        {
            int chaNum;
            //if (dt.Rows.Count < 30 * (count + 1))//即将下发的数据不足30 条了
            if (dt.Rows.Count <= 30 * (count + 1))//即将下发的数据不足30 条了
            {
                chaNum = dt.Rows.Count - 30 * count;
                if (!runState.isParalle)
                {
                    inCondition.isLast = true;
                    inCondition.lastNum = chaNum;
                }
                else
                {
                    paCondition.isLast = true;
                    paCondition.lastNum = chaNum;
                }
            }
            else
            {
                chaNum = 30;
            }
            if (runState.hasMoreData)//未发送完毕则发送下一笔工况包
            {
                if (!runState.isParalle)
                {
                    PreIndependConditionData(chaNum, dt, count, inCondition);
                    temp = inCondition.Command();
                    string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

                    StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

                }
                else
                {
                    PreParalleConditionData(chaNum, dt, count, paCondition, paraCount);
                    temp = paCondition.Command();
                    string[] str = Convert.ToString(client.RemoteEndPoint).Split(new char[] { ':' });

                    StartListen.queSendDic[str[0]].Enqueue(new SendInfo(temp, client));

                }
            }
            if (dt.Rows.Count <= 30 * (count + 1))
            //if(chaNum < 30)
            {
                runState.hasMoreData = false;
            }
            return runState;
        }
        /// <summary>
        /// 独立工况准备数据
        /// </summary>
        /// <param name="chaNum"></param>
        /// <param name="dt"></param>
        /// <param name="count"></param>
        /// <param name="inCondition"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependWorkCondition PreIndependConditionData(int chaNum, DataTable dt, int count, CommandProtocol.IndependWorkCondition inCondition)
        {
            int timeIndex = dt.Columns.Count;
            for (int i = 0; i < 30; i++)
            {
                if (i < chaNum)
                {
                    inCondition.ConditionParameter[i].param1 = Convert.ToSingle(dt.Rows[30 * count + i][0]);
                    if (timeIndex == 2)
                    {
                        inCondition.ConditionParameter[i].param2 = (ushort)(1000 * Convert.ToSingle(dt.Rows[30 * count + i][1]));
                        inCondition.ConditionParameter[i].param3 = 0;
                        inCondition.ConditionParameter[i].param4 = 0;
                    }
                    else
                    {
                        inCondition.ConditionParameter[i].param2 = (ushort)(10 * Convert.ToSingle(dt.Rows[30 * count + i][1]));
                        inCondition.ConditionParameter[i].param3 = Convert.ToSingle(dt.Rows[30 * count + i][2]);
                        inCondition.ConditionParameter[i].param4 = (ushort)(1000 * Convert.ToSingle(dt.Rows[30 * count + i][3]));
                    }
                }
                else
                {
                    inCondition.ConditionParameter[i].param1 = 0;
                    inCondition.ConditionParameter[i].param2 = 0;
                    inCondition.ConditionParameter[i].param3 = 0;
                    inCondition.ConditionParameter[i].param4 = 0;
                }
            }
            return inCondition;
        }
        /// <summary>
        /// 并联工况准备数据
        /// </summary>
        /// <param name="chaNum"></param>
        /// <param name="dt"></param>
        /// <param name="count"></param>
        /// <param name="paCondition"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleWorkCondition PreParalleConditionData(int chaNum, DataTable dt, int count, CommandProtocol.ParalleWorkCondition paCondition, int paraCount)
        {
            int timeIndex = dt.Columns.Count;
            for (int i = 0; i < 30; i++)
            {
                if (i < chaNum)
                {
                    paCondition.ConditionParameter[i].param1 = Convert.ToSingle(dt.Rows[30 * count + i][0]) / paraCount;
                    if (timeIndex == 2)
                    {
                        paCondition.ConditionParameter[i].param2 = (ushort)(1000 * Convert.ToSingle(dt.Rows[30 * count + i][1]));
                        paCondition.ConditionParameter[i].param3 = 0;
                        paCondition.ConditionParameter[i].param4 = 0;
                    }
                    else
                    {
                        paCondition.ConditionParameter[i].param2 = (ushort)(10 * Convert.ToSingle(dt.Rows[30 * count + i][1]));
                        paCondition.ConditionParameter[i].param3 = Convert.ToSingle(dt.Rows[30 * count + i][2]) / paraCount;
                        paCondition.ConditionParameter[i].param4 = (ushort)(1000 * Convert.ToSingle(dt.Rows[30 * count + i][3]));
                    }
                }
                else
                {
                    paCondition.ConditionParameter[i].param1 = 0;
                    paCondition.ConditionParameter[i].param2 = 0;
                    paCondition.ConditionParameter[i].param3 = 0;
                    paCondition.ConditionParameter[i].param4 = 0;
                }
            }
            return paCondition;
        }
        /// <summary>
        /// 开机校验时的操作
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="cs"></param>
        /// <param name="runState"></param>
        /// <returns></returns>
        public static StepRunBool OpenJudge(DateTime beginTime, StepRunBool runState, int trueCount, int paraCount)
        {
            DateTime nowTime = System.DateTime.Now;
            TimeSpan ts = new TimeSpan();
            TimeSpan maxTime = new TimeSpan(0, 0, 30);
            ts = nowTime.Subtract(beginTime);
            if (ts >= maxTime)
            {
                //Console.WriteLine("开机校验失败");
                //MessageBox.Show("DC接触器校验不通过，开机失败");
                runState.isOn = false;
                runState.isOpenJudge = false;
            }
            else
            {
                //Console.WriteLine("开机校验成功"+ts+" "+maxTime+" "+trueCount);
                if (runState.isParalle)
                {
                    if (trueCount == paraCount)
                    {
                        runState.isOn = true;
                        runState.isOpenJudge = false;
                    }
                }
                else
                {
                    if (trueCount == 1)
                    {
                        runState.isOn = true;
                        runState.isOpenJudge = false;
                    }
                }
            }
            return runState;
        }
        /// <summary>
        /// 根据间隔记录
        /// </summary>
        /// <param name="timeInterval"></param>
        /// <param name="volInterval"></param>
        /// <param name="currInterval"></param>
        /// <param name="recState"></param>
        /// <returns></returns>
        public static RecordState RecordByInterval(float timeInterval, float volInterval, float currInterval, RecordState recState, ChannelState cs, int seqId,int beginSeq)
        {
            //时间间隔达到
            if (timeInterval != 0)
            {
                float recordCount = timeInterval / 10;
                if (recState.count >= recordCount)
                {
                    //往通道数据表中插入一条新纪录
                    recState.needInsert = true;
                    //通过收包计数来计算相对时间
                    // recState.curTotalCount = recState.curTotalCount + recState.count;
                    //通过包序计算相对时间
                    //recState.curTotalCount = (ushort)(seqId - recState.beginSeq);
                    recState.curTotalCount = (ushort)(seqId - beginSeq);
                    //重置三个记录条件的起点
                    recState.count = 0;
                    recState.oldV = cs.DC_outputVolt;
                    recState.oldA = cs.DC_loadCurrent;
                }
            }
            //电压间隔达到
            if (volInterval != 0)
            {
                if (Math.Abs(cs.DC_outputVolt - recState.oldV) >= volInterval)
                {
                    recState.needInsert = true;
                    //通过收包计数来计算相对时间
                    // recState.curTotalCount = recState.curTotalCount + recState.count;
                    //通过包序计算相对时间
                    //recState.curTotalCount = (ushort)(seqId - recState.beginSeq);
                    recState.curTotalCount = (ushort)(seqId - beginSeq);
                    recState.oldV = cs.DC_outputVolt;
                    recState.oldA = cs.DC_loadCurrent;
                }
            }
            //电流间隔达到
            if (currInterval != 0)
            {
                if (Math.Abs(cs.DC_loadCurrent - recState.oldA) >= currInterval)
                {
                    recState.needInsert = true;
                    //通过收包计数来计算相对时间
                    //recState.curTotalCount = recState.curTotalCount + recState.count;
                    //通过包序计算相对时间
                    //recState.curTotalCount = (ushort)(seqId - recState.beginSeq);
                    recState.curTotalCount = (ushort)(seqId - beginSeq);
                    recState.count = 0;
                    recState.oldV = cs.DC_outputVolt;
                    recState.oldA = cs.DC_loadCurrent;
                }
            }
            return recState;
        }
        /// <summary>
        /// 准备给IndexPage中通道显示用的数据(除工步时间)
        /// </summary>
        /// <param name="chl"></param>
        /// <param name="stepSetInfo"></param>
        /// <param name="stepNum"></param>
        /// <param name="circ"></param>
        /// <param name="cs"></param>
        /// <param name="currentState"></param>
        /// <returns></returns>
        public static Channel PrepareDataToShow(Channel chl, StepSetInfo stepSetInfo, int stepNum, string circ, ChannelState cs, string currentState, StepRunBool runState)
        {
            chl.DevIp = stepSetInfo.DevIp;
            chl.StepId = stepNum;//工步序号
            chl.IterId = circ;//循环序号
            chl.Voltage = cs.DC_outputVolt;//电压
            chl.Current = cs.DC_loadCurrent;//电流
            chl.Capacity = cs.chargeCap;//充电容量
            chl.Energy = cs.chargeEnergy;//充电能量
            if (runState.isRunning == true)
            {
                chl.RunStat = true;//设置为运行状态
                chl.StopStat = false;//清掉单点停止的状态
            }
            else 
            {
                chl.RunStat = false;
                //if (runState.isClosed == false) 
                //{
                //    chl.StopStat = true;//设置为停止状态
                //}
                //else
                //{
                //    chl.StopStat = false;//设置为完成状态
                //}           
            }

            //CustomStep customStep = new CustomStep();
            //customStep = customStep.Spilt(currentState);//处理工步名称
            //chl.ChlState = (Channel.runningState)Enum.Parse(typeof(Channel.runningState), currentState);
            return chl;
        }
        /// <summary>
        /// 获取当前工步已经运行的时间
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="contiTime"></param>
        /// <param name="nowTime"></param>
        /// <param name="executionTime"></param>
        /// <returns></returns>
        public static TimeSpan GetStepRunTime(DateTime beginTime, DateTime contiTime, DateTime nowTime, TimeSpan executionTime)
        {
            TimeSpan ts = new TimeSpan();
            if (beginTime != default(DateTime))
            {
                //若该工步被单点暂停过，则不能计算暂停的那段时间
                if (contiTime != default(DateTime))
                {
                    ts = nowTime.Subtract(contiTime).Add(executionTime);
                    //Console.WriteLine("contiTime:" + contiTime);
                    //Console.WriteLine("nowTime:" + nowTime);
                    //Console.WriteLine("executionTime:" + executionTime);
                    //Console.WriteLine("ts:" + ts);
                }
                else
                {
                    ts = nowTime.Subtract(beginTime)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ;
                }
            }
            else
            {
                ts = new TimeSpan(0, 0, 0, 0, 0);
            }
            return ts;
        }
        /// <summary>
        /// 根据操作符判断跳转条件是否达成
        /// </summary>
        /// <param name="j"></param>
        /// <param name="jcList"></param>
        /// <param name="status"></param>
        /// <param name="jump"></param>
        /// <returns></returns>
        public static bool JudgeSuccess(int j, List<JumpCondition> jcList, float status, bool[] jump)
        {
            switch (jcList[j].JumpOperator)
            {
                case ">":
                    {
                        if (status > jcList[j].JumpValue)
                        {
                            jump[j] = true;
                        }
                    } break;
                case ">=":
                    {
                        if (status >= jcList[j].JumpValue)
                        {
                            jump[j] = true;
                        }
                    } break;
                case "=":
                    {
                        if (status == jcList[j].JumpValue)
                        {
                            jump[j] = true;
                        }
                    } break;
                case "<":
                    {
                        if (status < jcList[j].JumpValue)
                        {
                            jump[j] = true;
                        }
                    } break;
                case "<=":
                    {
                        if (status <= jcList[j].JumpValue)
                        {
                            jump[j] = true;
                        }
                    } break;
            }
            return jump[j];
        }
        /// <summary>
        /// 独立模式数据包通道指令参数设置
        /// </summary>
        /// <param name="inPara"></param>
        /// <param name="type"></param>
        /// <param name="index"></param>
        public static CommandProtocol.IndependParameter SetIndeParam(CommandProtocol.IndependParameter inPara, CustomStep customStep, int index, StepSetInfo stepSetInfo)
        {
            inPara = ClearIndeParam(inPara);
            switch (customStep.Type)
            {
                //恒阻模式的参数设置
                case 1:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].LoadResistor) * 100;
                    } break;
                //恒压恒流模式的参数设置
                case 2:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Current);
                        if (customStep.Dir)
                        {
                            inPara.param1 = -inPara.param1;
                        }
                        inPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].Voltage);
                    } break;
                //恒流阶跃模式的参数设置
                case 3:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Current);
                        if (customStep.Dir)
                        {
                            inPara.param1 = -inPara.param1;
                        }
                    } break;
                //恒压阶跃模式的参数设置
                case 4:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Voltage);
                    } break;
                //恒功率阶跃模式的参数设置
                case 5:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Power);
                        if (customStep.Dir)
                        {
                            inPara.param1 = -inPara.param1;
                        }
                    } break;
                //恒流斜坡模式的参数设置
                case 6:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].BeginValue);
                        inPara.param2 = (int)(Convert.ToSingle(stepSetInfo.StepList[index].Slope) * 10);
                        inPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].EndValue);
                        if (customStep.Dir)
                        {
                            inPara.param1 = -inPara.param1;
                            inPara.param3 = -inPara.param3;
                        }
                    } break;
                //恒压斜坡模式的参数设置
                case 7:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].BeginValue);
                        inPara.param2 = (int)(Convert.ToSingle(stepSetInfo.StepList[index].Slope) * 10);
                        inPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].EndValue);
                    } break;
                //恒流脉冲模式的参数设置
                case 8:
                    {
                        inPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Amplitude1);
                        inPara.param2 = Convert.ToUInt16(stepSetInfo.StepList[index].Time1);
                        inPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].Amplitude2);
                        inPara.param4 = Convert.ToUInt16(stepSetInfo.StepList[index].Time2);
                        if (customStep.Dir)
                        {
                            inPara.param1 = -inPara.param1;
                            inPara.param3 = -inPara.param3;
                        }
                    } break;
            }
            return inPara;
        }
        /// <summary>
        /// 并联模式数据包通道指令参数设置
        /// </summary>
        /// <param name="temp_StepDataIP"></param>
        /// <param name="type"></param>
        /// <param name="index"></param>
        public static CommandProtocol.ParalleParameter SetParaParam(CommandProtocol.ParalleParameter paPara, CustomStep customStep, int index, StepSetInfo stepSetInfo)
        {
            paPara = ClearParaParam(paPara);
            int count = stepSetInfo.ParallelList.Count;
            switch (customStep.Type)
            {
                //恒阻模式的参数设置
                case 1:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].LoadResistor) * count * 100;
                    } break;
                //恒压恒流模式的参数设置
                case 2:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Current) / count;
                        paPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].Voltage);
                        if (customStep.Dir)
                        {
                            paPara.param1 = -paPara.param1;
                        }
                    } break;
                //恒流阶跃模式的参数设置
                case 3:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Current) / count;
                        if (customStep.Dir)
                        {
                            paPara.param1 = -paPara.param1;
                        }
                    } break;
                //恒压阶跃模式的参数设置
                case 4:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Voltage);
                    } break;
                //恒功率阶跃模式的参数设置
                case 5:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Power) / count;
                        if (customStep.Dir)
                        {
                            paPara.param1 = -paPara.param1;
                        }
                    } break;
                //恒流斜坡模式的参数设置
                case 6:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].BeginValue) / count;
                        paPara.param2 = (int)(Convert.ToUInt16(stepSetInfo.StepList[index].Slope) * 10);
                        paPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].EndValue) / count;
                        if (customStep.Dir)
                        {
                            paPara.param1 = -paPara.param1;
                            paPara.param3 = -paPara.param3;
                        }
                    } break;
                //恒压斜坡模式的参数设置
                case 7:
                    {
                        paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].BeginValue);
                        paPara.param2 = (int)(Convert.ToUInt16(stepSetInfo.StepList[index].Slope) * 10);
                        paPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].EndValue);
                    } break;
                //恒流脉冲模式的参数设置
                case 8:
                    {
                        //恒流脉冲模式
                        if (customStep.Model == 1)
                        {
                            paPara.param1 = Convert.ToSingle(stepSetInfo.StepList[index].Amplitude1) / count;
                            paPara.param2 = Convert.ToUInt16(stepSetInfo.StepList[index].Time1);
                            paPara.param3 = Convert.ToSingle(stepSetInfo.StepList[index].Amplitude2) / count;
                            paPara.param4 = Convert.ToUInt16(stepSetInfo.StepList[index].Time2);
                        }
                        if (customStep.Dir)
                        {
                            paPara.param1 = -paPara.param1;
                            paPara.param3 = -paPara.param3;
                        }
                    } break;
            }
            return paPara;
        }

        /// <summary>
        /// 清空并联模式数据包的通道指令参数
        /// </summary>
        /// <param name="paPara"></param>
        private static CommandProtocol.ParalleParameter ClearParaParam(CommandProtocol.ParalleParameter paPara)
        {
            paPara.param1 = 0;
            paPara.param2 = 0;
            paPara.param3 = 0;
            paPara.param4 = 0;
            return paPara;
        }
        /// <summary>
        /// 清空独立模式数据包的通道指令参数
        /// </summary>
        /// <param name="inPara"></param>
        private static CommandProtocol.IndependParameter ClearIndeParam(CommandProtocol.IndependParameter inPara)
        {
            inPara.param1 = 0;
            inPara.param2 = 0;
            inPara.param3 = 0;
            inPara.param4 = 0;
            return inPara;
        }
        /// <summary>
        /// 设置独立参数包的工步类型和保护条件相关属性
        /// </summary>
        /// <param name="inPara"></param>
        /// <param name="stepSetInfo"></param>
        /// <param name="customStep"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static CommandProtocol.IndependParameter SetIndeStepParam(CommandProtocol.IndependParameter inPara, CustomStep customStep, int index, StepSetInfo stepSetInfo)
        {
            inPara.stepNum = index;
            inPara.commandUpdata = !inPara.commandUpdata;
            inPara.voltLimitLower = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.VoltageMin) + 20) * 1000);
            inPara.voltLimitUpper = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.VoltageMax) + 20) * 1000);
            inPara.currentLimitLower = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CurrentMin) + 150) * 100);
            inPara.currentLimitUpper = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CurrentMax) + 150) * 100);
            inPara.temperLimitUpper = (int)(2898 / (Math.Exp((1 / (Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.TemperatureMax) + 273.5) - 1 / 298.15) * 3470) + 0.4));
            if (stepSetInfo.StepList[index].ProtSelect.SelCustCurr)
            {
                inPara.clientLimit = true;
                inPara.clientLower = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CustomerCurrMin) + 150) * 100);
                inPara.clientUpper = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CustomerCurrMax) + 150) * 100);
            }
            inPara.dir = customStep.Dir;
            inPara.modelControl = customStep.Model - 1;
            inPara.waveControl = customStep.Wave - 1;
            return inPara;
        }
        /// <summary>
        /// 设置并联参数包的工步类型和保护条件相关属性
        /// </summary>
        /// <param name="paPara"></param>
        /// <param name="customStep"></param>
        /// <param name="index"></param>
        /// <param name="stepSetInfo"></param>
        /// <returns></returns>
        public static CommandProtocol.ParalleParameter SetParaStepParam(CommandProtocol.ParalleParameter paPara, CustomStep customStep, int index, StepSetInfo stepSetInfo)
        {
            int count = stepSetInfo.ParallelList.Count;
            paPara.stepNum = index;
            paPara.commandUpdata = !paPara.commandUpdata;
            paPara.voltLimitLower = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.VoltageMin) + 20) * 1000);
            paPara.voltLimitUpper = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.VoltageMax) + 20) * 1000);
            paPara.currentLimitLower = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CurrentMin) + 150) * 100);
            paPara.currentLimitUpper = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CurrentMax) + 150) * 100);
            paPara.temperLimitUpper = (int)(2898 / (Math.Exp((1 / (Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.TemperatureMax) + 273.5) - 1 / 298.15) * 3470) + 0.4));
            if (stepSetInfo.StepList[index].ProtSelect.SelCustCurr)
            {
                paPara.clientLimit = true;
                paPara.clientLower = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CustomerCurrMin) + 150) * 100);
                paPara.clientUpper = (int)((Convert.ToSingle(stepSetInfo.StepList[index].ProtCondition.CustomerCurrMax) + 150) * 100);
            }
            paPara.dir = customStep.Dir;
            paPara.modelControl = customStep.Model - 1;
            paPara.waveControl = customStep.Wave - 1;
            return paPara;
        }
        /// <summary>
        /// 将保护条件加入到跳转条件中
        /// </summary>
        /// <param name="stepSetInfo"></param>
        /// <param name="jcList"></param>
        /// <returns></returns>
        public static List<JumpCondition> GetProctCondition(StepSetInfo stepSetInfo, List<JumpCondition> jcList, int stepNum)
        {
            if (stepSetInfo.StepList[stepNum - 1].ProtSelect.SelVolMin == true)
            {
                JumpCondition jumpCond = new JumpCondition();
                jumpCond.JumpCond = "电池电压";
                jumpCond.JumpOperator = "<";
                jumpCond.JumpValue = Convert.ToSingle(stepSetInfo.StepList[stepNum - 1].ProtCondition.VoltageMin);
                jumpCond.JumpTo = "goto:完成";
                jcList.Add(jumpCond);
            }
            if (stepSetInfo.StepList[stepNum - 1].ProtSelect.SelVolMax == true)
            {
                JumpCondition jumpCond = new JumpCondition();
                jumpCond.JumpCond = "电池电压";
                jumpCond.JumpOperator = ">";
                jumpCond.JumpValue = Convert.ToSingle(stepSetInfo.StepList[stepNum - 1].ProtCondition.VoltageMax);
                jumpCond.JumpTo = "goto:完成";
                jcList.Add(jumpCond);
            }
            if (stepSetInfo.StepList[stepNum - 1].ProtSelect.SelCurrMin == true)
            {
                JumpCondition jumpCond = new JumpCondition();
                jumpCond.JumpCond = "电池电流";
                jumpCond.JumpOperator = "<";
                jumpCond.JumpValue = Convert.ToSingle(stepSetInfo.StepList[stepNum - 1].ProtCondition.CurrentMin);
                jumpCond.JumpTo = "goto:完成";
                jcList.Add(jumpCond);
            }
            if (stepSetInfo.StepList[stepNum - 1].ProtSelect.SelCurrMax == true)
            {
                JumpCondition jumpCond = new JumpCondition();
                jumpCond.JumpCond = "电池电流";
                jumpCond.JumpOperator = ">";
                jumpCond.JumpValue = Convert.ToSingle(stepSetInfo.StepList[stepNum - 1].ProtCondition.CurrentMax);
                jumpCond.JumpTo = "goto:完成";
                jcList.Add(jumpCond);
            }
            return jcList;
        }
        /// <summary>
        /// 将截至电流加入到跳转条件中
        /// </summary>
        /// <param name="stepSetInfo"></param>
        /// <param name="jcList"></param>
        /// <returns></returns>
        public static List<JumpCondition> GetCutOffCurrCondition(float cutoffCurr, StepSetInfo stepSetInfo, List<JumpCondition> jcList, int index)
        {
            int stepNum = index + 1;
            JumpCondition jumpCondCurr = new JumpCondition();
            jumpCondCurr.JumpCond = "电池电流";
            jumpCondCurr.JumpOperator = "<";
            jumpCondCurr.JumpValue = cutoffCurr;
            jumpCondCurr.JumpTo = "与(&)";
            jcList.Add(jumpCondCurr);
            JumpCondition jumpCondTime = new JumpCondition();
            jumpCondTime.JumpCond = "工步时间";
            jumpCondTime.JumpOperator = ">";
            jumpCondTime.JumpValue = 1;
            jumpCondTime.JumpTo = "goto:" + (stepNum + 1);
            jcList.Add(jumpCondTime);
            return jcList;
        }
        /// <summary>
        /// 获取工步的条件工步
        /// </summary>
        /// <param name="stepSetInfo"></param>
        /// <param name="index"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static JumpCondition GetStepJumpCondition(StepSetInfo stepSetInfo, int index, int i)
        {
            JumpCondition tempStepJumpCondition = new JumpCondition();
            tempStepJumpCondition.JumpCond = stepSetInfo.StepList[index].ChildList[i].JumpCond;
            tempStepJumpCondition.JumpOperator = stepSetInfo.StepList[index].ChildList[i].JumpOperator;
            tempStepJumpCondition.JumpValue = Convert.ToSingle(stepSetInfo.StepList[index].ChildList[i].JumpValue);
            tempStepJumpCondition.JumpTo = stepSetInfo.StepList[index].ChildList[i].JumpTo;
            return tempStepJumpCondition;
        }
        /// <summary>
        /// 获取工步的记录条件工步
        /// </summary>
        /// <param name="stepSetInfo"></param>
        /// <param name="index"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static RecordCondition GetStepRecordCondition(StepSetInfo stepSetInfo, int index, int i)
        {
            RecordCondition tempStepRecordCondition = new RecordCondition();
            tempStepRecordCondition.RecordCond = stepSetInfo.StepList[index].ChildList[i].RecordCond;
            tempStepRecordCondition.CondLower = stepSetInfo.StepList[index].ChildList[i].CondLower;
            tempStepRecordCondition.CondUpper = stepSetInfo.StepList[index].ChildList[i].CondUpper;
            tempStepRecordCondition.TimeInterval = stepSetInfo.StepList[index].ChildList[i].TimeInterval;
            tempStepRecordCondition.VolInterval = stepSetInfo.StepList[index].ChildList[i].VolInterval;
            tempStepRecordCondition.CurrInterval = stepSetInfo.StepList[index].ChildList[i].CurrInterval;
            return tempStepRecordCondition;
        }
        /// <summary>
        /// 组装循环号
        /// </summary>
        /// <returns></returns>
        public static string MakeCircStr(Dictionary<int, int> circDic, List<int> circList, StepSetInfo stepSetInfo)
        {
            string circStr = "";
            if (circDic.Count == 0)
            {
                circStr = "1";
            }
            else
            {
                for (int i = 0; i < circList.Count; i++)
                {
                    if (i != 0)
                    {
                        circStr = (circDic[circList[i]] + 1) + "-" + circStr;
                    }
                    else
                    {
                        circStr = (circDic[circList[i]] + 1) + circStr;
                    }
                }
            }
            return circStr;
        }
        /// <summary>
        /// 开机合闸失败，弹窗提示
        /// </summary>
        /// <param name="state"></param>
        public static void OpenShowMsg(object state)
        {
            StepSetInfo stepSetInfo = (StepSetInfo)state;
            MessageBox.Show("通道 " + stepSetInfo.Mid + "-" + stepSetInfo.Cid + " 开机合闸失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }
        /// <summary>
        /// 开机遇到故障，弹窗提示
        /// </summary>
        /// <param name="state"></param>
        public static void OpenErrorShowMsg(object state)
        {
            StepSetInfo stepSetInfo = (StepSetInfo)state;
            MessageBox.Show("通道 " + stepSetInfo.Mid + "-" + stepSetInfo.Cid + " 开机遇到通道故障", "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }
        /// <summary>
        /// 工况请求未收到，弹窗提示
        /// </summary>
        /// <param name="state"></param>
        public static void CondiShowMsg(object state)
        {
            StepSetInfo stepSetInfo = (StepSetInfo)state;
            MessageBox.Show("通道 " + stepSetInfo.Mid + "-" + stepSetInfo.Cid + " 未收到工况请求", "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }
        /// <summary>
        /// 丢包严重，弹窗提示
        /// </summary>
        /// <param name="state"></param>
        public static void LosePackMsg(object state)
        {
            StepSetInfo stepSetInfo = (StepSetInfo)state;
            MessageBox.Show(stepSetInfo.Gid + "号柜的" + stepSetInfo.Mid + "号中位机丢包严重，已强制关机", "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }

        /// <summary>
        /// 给dataTable设置列值
        /// </summary>
        /// <param name="dt"></param>
        public static void setDataTableColumn(DataTable dt)
        {
            dt.Columns.Add("seq_id", typeof(int));
            dt.Columns.Add("pulse_num", typeof(int));
            dt.Columns.Add("test_id", typeof(string));
            dt.Columns.Add("test_mode", typeof(int));
            dt.Columns.Add("mid_id", typeof(int));
            dt.Columns.Add("cabinet_id", typeof(int));
            dt.Columns.Add("channel_num", typeof(string));
            dt.Columns.Add("isAll", typeof(int));
            dt.Columns.Add("step_num", typeof(int));
            dt.Columns.Add("step_type", typeof(int));
            dt.Columns.Add("work_type", typeof(int));
            dt.Columns.Add("cycle", typeof(string));
            dt.Columns.Add("test_time", typeof(int));
            dt.Columns.Add("Channel_efficiency", typeof(int));
            dt.Columns.Add("DC_outputVolt", typeof(float));
            dt.Columns.Add("DC_loadCurrent", typeof(float));
            dt.Columns.Add("Load_temper1", typeof(float));
            dt.Columns.Add("Load_temper2", typeof(float));
            dt.Columns.Add("DC_busVolt", typeof(float));
            dt.Columns.Add("Charge_cap", typeof(float));
            dt.Columns.Add("Discharge_cap", typeof(float));
            dt.Columns.Add("Total_cap", typeof(float));
            dt.Columns.Add("Step_time", typeof(float));
            dt.Columns.Add("Charge_energy", typeof(float));
            dt.Columns.Add("Discharge_energy", typeof(float));
            dt.Columns.Add("Total_energy", typeof(float));
            dt.Columns.Add("power", typeof(float));
            dt.Columns.Add("Diff_volt", typeof(float));
            dt.Columns.Add("Diff_current", typeof(float));
            dt.Columns.Add("Aux_data", typeof(string));
            dt.Columns.Add("Status_data", typeof(string));
            dt.Columns.Add("Sys_time", typeof(string));
            dt.Columns.Add("isExtra", typeof(int));
        }
        
        /// <summary>
        /// 给dataTable设置行值
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="channelState"></param>
        /// <param name="circ"></param>
        /// <param name="testid"></param>
        /// <param name="testmode"></param>
        /// <param name="stepNum"></param>
        /// <param name="stepType"></param>
        /// <param name="workType"></param>
        /// <param name="testtime"></param>
        /// <param name="paStr"></param>
        /// <param name="isExtraData"></param>
        /// <param name="auxData"></param>
        public static void setDataTableRows(DataTable dt, ChannelState channelState, string circ, string testid, int testmode, int stepNum, int stepType, int workType, int testtime, string paStr, bool isExtraData, List<AuxChannel> auxData)
        {
            string cid;
            int isAll;
            int isExtra;
            if (paStr == "")
            {
                cid = Convert.ToString(channelState.CID + 1);
                isAll = 0;
            }
            else
            {
                cid = paStr;
                isAll = channelState.CID + 1;
            }
            if (isExtraData)
            {
                isExtra = 1;
            }
            else
            {
                isExtra = 0;
            }
            //StringBuilder sb = new StringBuilder();
            //sb.Append("Aux_data=" + JsonHelper.Serialize(auxData) + "/");
            //sb.Append("ChannelEfficiency=" + channelState.channelEfficiency + "/");
            //sb.Append("Charge_cap=#" + channelState.chargeCap + "#/");
            //sb.Append("Charge_energy=" + channelState.chargeEnergy + "/");
            //sb.Append("CID=" + cid + "/");
            //sb.Append("Cycle=" + circ + "/");
            //sb.Append("DC_busVolt=" + channelState.DC_busVolt + "/");
            //sb.Append("DC_loadCurrent=" + channelState.DC_loadCurrent + "/");
            //sb.Append("DC_outputVolt=" + channelState.DC_outputVolt + "/");
            //sb.Append("Diff_current=" + channelState.diffCurrent + "/");
            //sb.Append("Diff_volt=" + channelState.diffVolt + "/");
            //sb.Append("Discharge_cap=@" + channelState.dischargeCap + "@/");
            //sb.Append("Discharge_energy=" + channelState.dischargeEnergy + "/");
            //sb.Append("GID=" + channelState.GID + 1 + "/");
            //sb.Append("IsAll=" + isAll + "/");
            //sb.Append("IsExtra=" + isExtra + "/");
            //sb.Append("Load_temper1=" + channelState.DC_loadTemper + "/");
            //sb.Append("Load_temper2=" + channelState.DC_ambientTemper + "/");
            //sb.Append("Power=" + channelState.power + "/");
            ////sb.Append("SeqId=" + (firstSeqID + count) + "/");
            //sb.Append("Status_data=" + channelState.faultStatus + "/");
            //sb.Append("Step_time=" + channelState.stepTime + "/");
            //sb.Append("StepName=" + DicHelper.RetrieveStepName(stepType) + "/");
            //sb.Append("StepNum=" + stepNum + "/");
            //sb.Append("StepType=" + stepType + "/");
            //sb.Append("Sys_Time=" + channelState.time + "/");
            //sb.Append("TestId=" + testid + "/");
            //sb.Append("TestMode=" + testmode + "/");
            //sb.Append("TestTime=" + testtime + "/");
            //sb.Append("Total_cap=%" + channelState.totalCap + "%/");
            //sb.Append("Total_energy=" + channelState.totalEnergy + "/");
            //sb.Append("WorkType=" + workType + "/");
            //sb.Append("PulseNum=&" + channelState.pulseNumber + "&/}");
            dt.Rows.Add(DBNull.Value, channelState.pulseNumber, testid, testmode, channelState.MID + 1, channelState.GID + 1, cid, isAll, stepNum, stepType, workType, circ, testtime, channelState.channelEfficiency, channelState.DC_outputVolt, channelState.DC_loadCurrent, channelState.DC_loadTemper, channelState.DC_ambientTemper, channelState.DC_busVolt,
                         channelState.chargeCap, channelState.dischargeCap, channelState.totalCap, channelState.stepTime, channelState.chargeEnergy, channelState.dischargeEnergy, channelState.totalEnergy, channelState.power, channelState.diffVolt, channelState.diffCurrent, JsonHelper.Serialize(auxData), channelState.faultStatus, channelState.time, isExtra);
        }
    }
}
